package com.edu.neusoft.student.controller;

import com.edu.neusoft.student.service.StudentService;
import com.edu.neusoft.student.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.edu.neusoft.student.domain.Student;
import java.util.List;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/student")
@Api(tags = "学生管理接口", description = "提供对学生的增删改查操作")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @DeleteMapping("/{studentId}")
    @ApiOperation(value = "根据主键删除学生信息", notes = "根据传入的学生ID删除对应的学生信息")
    public AjaxResult deleteByPrimaryKey(@PathVariable Integer studentId) {
        int result = studentService.deleteByPrimaryKey(studentId);
        return result > 0 ? AjaxResult.success("成功") : AjaxResult.error("删除失败");
    }

    @PostMapping
    @ApiOperation(value = "插入学生信息", notes = "插入一条新的学生信息")
    public AjaxResult insert(@RequestBody Student student) {
        int result = studentService.insert(student);
        return result > 0 ? AjaxResult.success("成功") : AjaxResult.error("插入失败");
    }

    @PostMapping("/selective")
    @ApiOperation(value = "选择性插入学生信息", notes = "只插入非空字段的学生信息")
    public AjaxResult insertSelective(@RequestBody Student student) {
        int result = studentService.insertSelective(student);
        return result > 0 ? AjaxResult.success("成功") : AjaxResult.error("选择性插入失败");
    }

    @GetMapping("/{studentId}")
    @ApiOperation(value = "根据主键查询学生信息", notes = "根据传入的学生ID查询对应的学生信息")
    public AjaxResult selectByPrimaryKey(@PathVariable Integer studentId) {
        Student student = studentService.selectByPrimaryKey(studentId);
        return student != null ? AjaxResult.success(student) : AjaxResult.error("未找到学生信息");
    }

    @PutMapping("/selective")
    @ApiOperation(value = "选择性更新学生信息", notes = "只更新非空字段的学生信息")
    public AjaxResult updateByPrimaryKeySelective(@RequestBody Student student) {
        int result = studentService.updateByPrimaryKeySelective(student);
        return result > 0 ? AjaxResult.success("成功") : AjaxResult.error("选择性更新失败");
    }

    @PutMapping
    @ApiOperation(value = "根据主键更新学生信息", notes = "更新对应学生ID的所有学生信息")
    public AjaxResult updateByPrimaryKey(@RequestBody Student student) {
        int result = studentService.updateByPrimaryKey(student);
        return result > 0 ? AjaxResult.success("成功") : AjaxResult.error("更新失败");
    }

    @GetMapping
    @ApiOperation(value = "查询所有学生信息", notes = "根据条件查询所有符合条件的学生信息，条件可以为空")
    public AjaxResult selectByAll(@RequestBody(required = false) Student student) {
        List<Student> students = studentService.selectByAll(student);
        return students != null ? AjaxResult.success(students) : AjaxResult.error("查询失败");
    }
}